package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.FoodList;
import com.enbatis.mybatisplugs.service.FoodListService;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;

@RestController
@RequestMapping("v1/food_list")
public class FoodListController extends BaseController {

@Autowired
private FoodListService foodListService;


    @PostMapping("/add")
    public ResultReturn<FoodList> add(@RequestBody FoodList foodList){
        return  success(foodListService.insert(foodList));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(foodListService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody FoodList foodList){
        foodList.setId(id);
        return success(foodListService.updateById(foodList));
    }


    @GetMapping("/{id}")
    public ResultReturn<FoodList> get(@PathVariable("id") Long id) {
        return success(foodListService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<FoodList>> getList(FoodList foodList){
        return success(foodListService.list(new Wrapper<>(foodList)));
    }


    @PostMapping(value = "/upload")
    public ResultReturn uploadFoodList(@RequestParam("file") MultipartFile file){
        return success(foodListService.uploadFoodList(file,getAccount()));
    }

    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page")
    public ResultReturn<Pages<FoodList>> getPage(@RequestBody FoodList category){
        Page<FoodList> page= getPage();
        return success(foodListService.page(page,new Wrapper<>(category).orderBy("what_day",true)));
    }


    @ApiOperation(value = "轧钢检测结果导出", notes = "炼铁合炉检测结果导出")
    @PostMapping("/export/")
    public void export( @RequestParam("id") Long id){
        foodListService.export(response,id);
    }

}
